<?php


namespace app\manager\controller;


use app\common\controller\Common;
use app\common\model\Site;
use think\facade\App;

class Siteset extends Common
{
    public function get()
    {
        $reSite = Site::where('s_id', '=', 1)->find();
        if (empty($reSite)) return $this->errJson('暂无信息');
        $this->assign('SiteInfo', $reSite);
        return view('set');
    }

    public function uploads()
    {
        $s_id =1 ;//input('merid');
        $ret = $this->upfile('file', [750, 370]);
        if (isset($ret['code']) && $ret['code'] == 2) {
            $reSite = Site::where('s_id', '=', $s_id)->find();
            $images = $reSite->s_banner;
            if ($images == NULL || $images == "") {
                $images = array();
            } else {
                $images = explode(',', $images);
            }
            if (count($images) < 3) {//限制上传三张以内
                array_push($images, $ret['thumb']);
                $reSite->s_banner = implode(',', $images);
                $res = $reSite->save();
                if ($res) return $this->okJson('上传成功', $ret);
            }
        }
        return $this->errJson('上传失败');
    }

    //删除文件
    public function delfile()
    {
        $path = input('path');
        if (empty($path)) return $this->errJson('路径不能为空');
        $dir=App::getRootPath();
        $res = unlink($dir.'public'.$path);
        if ($res){
            $reSite= Site::where('s_id','=',1)->find();
            $imgs = $reSite->s_banner;
            $imgs=explode(',',$imgs);
            $key = array_search($path, $imgs);
            if ($key !== false){
                array_splice($imgs, $key, 1);
                $reSite->s_banner=implode(',',$imgs);
                $ret = $reSite->save();
                if ($ret) return $this->okJson('图片删除成功');
            }
        }
        return $this->errJson('图片删除失败');
    }


    public function setInfo(){
        $param = request()->param();
        unset($param['file']);
        $ret = Site::where('s_id', '=', 1)->update($param);
        if ($ret) return $this->okJson('更新成功');
        return $this->errJson('更新失败');
    }
}